﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///Promotion 的摘要说明
/// </summary>
public class Promotion
{
	public Promotion()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    private int promotionid;

    public int PromotionId
    {
        get { return promotionid; }
        set { promotionid = value; }
    }
    private string promotiontitle;

    public string PromotionTitle
    {
        get { return promotiontitle; }
        set { promotiontitle = value; }
    }
    private DateTime startday;

    public DateTime StartDay
    {
        get { return startday; }
        set { startday = value; }
    }
    private DateTime dueday;

    public DateTime DueDay
    {
        get { return dueday; }
        set { dueday = value; }
    }
    private string pictureaname;

    public string PictureAName
    {
        get { return pictureaname; }
        set { pictureaname = value; }
    }
    private string picturebname;

    public string PictureBName
    {
        get { return picturebname; }
        set { picturebname = value; }
    }

    public string PromotionStatus
    {
        get
        {
            if (dueday < DateTime.Now)
            {
                return "overdue";
            }

            if (startday > DateTime.Now)
            {
                return "notstart";
            }

            if (startday < DateTime.Now && dueday>DateTime.Now)
            {
                return "inprocess";
            }

            return "invalid";
        }
    }

    public int RemainingDays
    {
        get
        {
            if (PromotionStatus == "inprocess")
            {
                return (dueday - DateTime.Now).Days+1;
            }
            return 0;
        }
    }

    public static Promotion ConvertToPromotion(DataRow dr)
    {
        if (dr == null)
            return null;
        Promotion promotion = new Promotion();
        if (dr["PromotionId"] != null)
            promotion.PromotionId = Convert.ToInt32(dr["PromotionId"]);
        if (dr["PromotionTitle"] != null)
            promotion.PromotionTitle = Convert.ToString(dr["PromotionTitle"]);
        if (dr["StartDay"] != null)
            promotion.StartDay = Convert.ToDateTime(dr["StartDay"]);
        if (dr["DueDay"] != null)
            promotion.DueDay = Convert.ToDateTime(dr["DueDay"]);
        if (dr["PictureAName"] != null)
            promotion.PictureAName = Convert.ToString(dr["PictureAName"]);
        if (dr["PictureBName"] != null)
            promotion.PictureBName = Convert.ToString(dr["PictureBName"]);

        return promotion;
    }

    public static IList<Promotion> ConvertToPromotionList(DataTable dt)
    {
        List<Promotion> promotionlist = new List<Promotion>();
        foreach (DataRow dr in dt.Rows)
        {
            Promotion promotion = ConvertToPromotion(dr);
            if (promotion != null)
                promotionlist.Add(promotion);
        }
        return promotionlist;
    }
}
